package swingExtended;

import java.awt.Component;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Shimu
 * @date 9-Jan-2013
 */
public class SwingExtended {

    protected SwingExtended() {
    }

    /**
     * Opens a warning window which displays the given warning text and 
     * an OK button.
     * @param parent
     * @param warningText 
     */
    public static void openWarningWindow(Component parent, String warningText) {
        JOptionPane.showMessageDialog(parent, warningText);
    }

    /**
     * Opens a warning window which displays the given warning text, the
     * name of the given Exception, and an OK button.
     * @param parent
     * @param warningText
     * @param ex 
     */
    public static void openWarningWindow(Component parent, String warningText, 
            Exception ex) {
        openWarningWindow(parent, String.format("%s\n%s",
                warningText, ex.toString()));
    }
    
    public static void openWarningWindow(Component parent, String warningText,
            Error e) {
        openWarningWindow(parent, String.format("%s\n%s",
                warningText, e.toString()));
    }

    /**
     * Opens a dialog window which displays the given text and returns true
     * if the user selects a Yes or OK option, returns false otherwise.
     * 
     * @param parent the parent component
     * @param text the text to be displayed
     * @param optionType an integer designating the options available on 
     * the dialog: JOptionPane.YES_NO_OPTION, YES_NO_CANCEL_OPTION, or OK_CANCEL_OPTION
     * @return 
     */
    public static boolean openConfirmWindow(Component parent, String text,
            int optionType) {
        int result = JOptionPane.showConfirmDialog(parent, text, 
                "Confirmation", optionType);
        return ((result == JOptionPane.YES_OPTION) 
                || (result == JOptionPane.OK_OPTION));
    }
    
    /**
     * Opens up a dialog window and returns the file the user selected.
     * Null if the selection was canceled.
     * 
     * @param parent the parent component of the JFileChooser
     * @return a File object representing the file the user selected.
     */
    public static File openBrowseWindow(Component parent) {
        final JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(parent);
        return chooser.getSelectedFile();
    }

    /**
     * Opens up a dialog window and returns the file the user selected.
     * Null if the selection was canceled.
     * 
     * @param startUrl the directory the browse window starts in
     * @param parent the parent component of the JFileChooser 
     * @throws FileNotFoundException if the given startUrl is invalid
     * @return a File object representing the file the user selected.
     */
    public static File openBrowseWindow(String startUrl, Component parent)
            throws FileNotFoundException {
        File startDirectory = new File(startUrl);
        if (!startDirectory.exists()) {
            throw new FileNotFoundException("The given start url is invalid!");
        }
        final JFileChooser chooser = new JFileChooser(startDirectory);
        chooser.showOpenDialog(parent);
        return chooser.getSelectedFile();
    }
    
    /**
     * Run the given Runnable and catching all exceptions.
     * 
     * @param parent
     * @param runnable 
     */
    public static void run(Component parent, Runnable runnable) {
        try {
            runnable.run();
        } catch (Exception e) {
            openWarningWindow(parent, "An error occurred!", e);
        }
    }
}
